[How-to] Make a working xml file in php (encoding properly) to export data from online database to i

Posted by gotye on Stack Overflow See other posts from Stack Overflow or by gotye
Published on 2010-03-13T12:43:54Z Indexed on 2010/03/13 12:45 UTC
Read the original article Hit count: 333

Filed under:
|
|
|
|

Hey guys,

I am trying to make an xml file to export my data from my database to my iphone. Each time I create a new post, I need to execute a php file to create a xml file containing the latest post ;)

Okay so far ? :D

Here is current php code ... but my nsxmlparser gives me an error code (33 - String is not started). I have no idea what kind of php functions I have to use ...

<?php

// Èdition du dÈbut du fichier XML
$xml .= '<?xml version=\"1.0\" encoding=\"UTF-8\"?>';
$sml .= '<channel>'; 
$xml .= '<title>Infonul</title>';
$xml .= '<link>aaa</link>';
$xml .= '<description>aaa</description>';


// connexion a la base (mettre ‡ jour le mdp)
$connect = mysql_connect('...-12','...','...');


/* selection de la base de donnÈe mysql */
mysql_select_db('...');


// selection des 20 derniËres news
$res=mysql_query("SELECT u.display_name as author,p.post_date as date,p.comment_count as commentCount, p.post_content as content,p.post_title as title FROM wp_posts p, wp_users u WHERE p.post_status = 'publish' and p.post_type = 'post' and p.post_author = u.id ORDER BY p.id DESC LIMIT 0,20");




// extraction des informations et ajout au contenu
while($tab=mysql_fetch_array($res)){   

 $title=$tab[title];
 $author=$tab[author];
 $content=$tab[content]; //html stuff
 $commentCount=$tab[commentCount];
 $date=$tab[date];

 $xml .= '<item>';
 $xml .= '<title>'.$title.'</title>';
 $xml .= '<content><![CDATA['.$content.']]></content>';
 $xml .= '<date>'.$date.'</date>';
 $xml .= '<author>'.$author.'</author>';
 $xml .= '<commentCount>'.$commentCount.'</commentCount>';
 $xml .= '</item>'; 
}

// Èdition de la fin du fichier XML
$xml .= '</channel>';
$xml = utf8_encode($xml);

echo $xml;

// Ècriture dans le fichier
if ($fp = fopen("20news.xml",'w'))
{
 fputs($fp,$xml);
 fclose($fp);
}

//mysql_close();

?>

I noticed a few things when i open 20news.xml in my browser :

  • I got squares instead of single quotes ...
  • I can't see <[CDATA[ but ]]> is clearly visible ... why ?!?

Thanks for any input ;)

Gotye.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about nsxmlparser